﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wicresoft.UnifyShow.Business;
using Oracle.DataAccess.Client;
using System.Data;

namespace Wicresoft.UnifyShow.BusinessImpl
{
    public class SiteBranchAndDeviceAndAlert : Basic, ISiteBranchAndDeviceAndAlert
    {
        public SiteBranchAndDeviceTypeAlertModel getSiteBranchAndDeviceAndAlert(int siteID)
        {
            return getSiteBranchAndDeviceAndAlert(siteID, null, null);
        }

        public SiteBranchAndDeviceTypeAlertModel getSiteBranchAndDeviceAndAlert(int siteID, int? userID)
        {
            return getSiteBranchAndDeviceAndAlert(siteID, null, userID);
        }

        public SiteBranchAndDeviceTypeAlertModel getSiteBranchAndDeviceAndAlert(int siteID, int? businesssystemID, int? userID)
        {
            SiteBranchAndDeviceTypeAlertModel model = new SiteBranchAndDeviceTypeAlertModel();
            string sqlStr = string.Empty;
            if (userID.HasValue)
            {
                sqlStr = "select a.* from sitebranch a, usersite b, user_x c where a.id = b.siteid and b.userid = c.id and (b.disabled=0 or b.disabled is null) and b.userid=:userid and b.siteid = :siteid";
            }
            else
            {
                sqlStr = "select a.* from sitebranch a where a.id = :siteid";
            }
            List<OracleParameter> cmdParms = new List<OracleParameter>();
            if (userID.HasValue)
            {
                cmdParms.Add(new OracleParameter("userid", OracleDbType.Int32, userID, ParameterDirection.Input));
            }
            cmdParms.Add(new OracleParameter("siteid", OracleDbType.Int32, siteID, ParameterDirection.Input));
            OracleDataReader odr = oda.ExecuteReader(sqlStr, cmdParms);
            SiteBranchModel sbm = new SiteBranchModel();
            List<DeviceTypeAlertModel> dtam = new List<DeviceTypeAlertModel>();
            sbm = ModelHelper<SiteBranchModel>.GetModel_By_OracleDataReader(odr).FirstOrDefault();
            if (sbm != null)
            {
                string str = " siteid=:siteid ";
                List<OracleParameter> cmdParms2 = new List<OracleParameter>();
                cmdParms2.Add(new OracleParameter("siteid", OracleDbType.Int32, siteID, ParameterDirection.Input));
                if (businesssystemID != null)
                {
                    str = string.Concat(str, " and businesssystemid=:businesssystemid ");
                    cmdParms2.Add(new OracleParameter("businesssystemid", OracleDbType.Int32, businesssystemID, ParameterDirection.Input));
                }
                string str2 = " siteid=:siteid2 ";
                cmdParms2.Add(new OracleParameter("siteid2", OracleDbType.Int32, siteID, ParameterDirection.Input));
                if (businesssystemID != null)
                {
                    str2 = string.Concat(str2, " and businesssystemid=:businesssystemid2 ");
                    cmdParms2.Add(new OracleParameter("businesssystemid2", OracleDbType.Int32, businesssystemID, ParameterDirection.Input));
                }
                string sqlStr2 = string.Concat("select t1.type, t1.count, t2.alertCount from (select count(*) Count, type from device where ", str,
                    " group by type) t1 left join (select count(*) alertCount,type from device t1 , alert t2 where ", str2,
                    " and t1.id=t2.deviceid and t2.status='OPEN' group by type) t2 on t1.type = t2.type");
                OracleDataReader odr2 = oda.ExecuteReader(sqlStr2, cmdParms2);
                dtam = ModelHelper<DeviceTypeAlertModel>.GetModel_By_OracleDataReader(odr2);
                model.SiteBranch = sbm;
                model.DeviceTypeAlert = dtam;
            }
            else
            {
                model.SiteBranch = new SiteBranchModel();
                model.DeviceTypeAlert = new List<DeviceTypeAlertModel>();
            }
           
            return model;
        }

        
    }
}
